package videosearch;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.logging.Logger;

// High level class which creates / maintains the gui and other threads
public class MyQuery {

    public static void main(String[] args) {
        ArrayList<VideoProcessor> database = null;
        BufferedReader input = null;
        PrintWriter output = null;

        // Case statement to check command line if we're importing or exporting
        if (args[0].equalsIgnoreCase("1")) {
            // Use the following code to generate the database
            try {
                output = new PrintWriter(
                        new BufferedWriter(
                        new FileWriter("db.csv", true)));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MyQuery.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MyQuery.class.getName()).log(Level.SEVERE, null, ex);
            }

            // Loop through the remaining command arguments, characterize them,
            // and write out the results to the file
            for (int i = 1; i < args.length; i++) {
                VideoProcessor vp = new VideoProcessor(args[i]);
                vp.write(output);
                System.out.println("Completed Processing on " + args[i]);
            }
            output.close();
            return;
        }

        // Only get here if it's not in import mode.
        // Create a GUI and the database to query from.
        Gui mGUI = new Gui(args[1], args[2]);
        database = new ArrayList();

        // Read in the database file and 
        try {
            input = new BufferedReader(new FileReader("db.csv"));

            String line = null;
            while ((line = input.readLine()) != null) {
                String[] video = line.split(",", 2);
                database.add(new VideoProcessor(video[0], video[1]));
            }
        } catch (FileNotFoundException ex) {
            System.out.println("Couldn't find the database file, "
                    + "make sure db.csv is in the working directory.");
            Logger.getLogger(MyQuery.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MyQuery.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Create new videoprocessor for the query clip
        ArrayList<Pair> results = new ArrayList<>();
        VideoProcessor query = new VideoProcessor(args[1]);
        // For each video in the database, perform the find function to check
        // for possible matches
        for (VideoProcessor video : database) {
            // matches contains a list of matches based oon frame
            ArrayList<Double> matches = video.find(query);
            // collect the results against each video
            results.add(new Pair(video.fileName, matches));
        }
        // Sort them  because that's the fastest way of finding the top 3
        Collections.sort(results, new Comparator<Pair>() {
            @Override
            public int compare(Pair left, Pair right) {
                return (int) (left.min - right.min);
            }
        });

        // Set the matches found in the gui
        mGUI.setMatches(results);

        // The rest is based on user input...
    }
}
